using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace MagicVoxelParse
{
    public static class TParse
    {
        public static void ParseFile(string path,GameObject meshDrawer)
        {
            try
            {
                //解析Voxel
                var stream = new FileInfo(path).OpenRead();
                var buffer = new byte[stream.Length];
                stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
                stream.Close();
                stream.Dispose();
                
                var dataHolder = new TDataHolder(buffer);
                TDataHolder.Init(dataHolder);
                dataHolder.data.size = dataHolder.data.anim[0].size;
                dataHolder.data.voxel = dataHolder.data.anim[0].voxel;

                if (dataHolder.data.palette.Count == 0)
                {
                    Debug.Log("(use default palette)");
                    dataHolder.data.palette = DefaultPalette;
                }
                else
                {
                    var len = dataHolder.data.palette.Count;
                    dataHolder.data.palette.Insert(0,dataHolder.data.palette[0]);
                    dataHolder.data.palette.RemoveAt(len);
                }
                
                //构建Mesh
                var meshBuild = new TMeshBuilder {renderer = meshDrawer};
                meshBuild.Init(dataHolder.data,new VoxelParam
                {
                    voxelSize = 1,
                    optimizeFaces = true,
                    vertexColor = true
                });
            }
            catch (Exception e)
            {
                MTLog.LogException(e);
            }
        }

        private static readonly List<PaletteStruct> DefaultPalette = new List<PaletteStruct>
        {
            new PaletteStruct {r= 255, g= 255, b= 255, a= 255},
            new PaletteStruct {r= 255, g= 255, b= 255, a= 255},
            new PaletteStruct {r= 255, g= 255, b= 204, a= 255},
            new PaletteStruct {r= 255, g= 255, b= 153, a= 255},
            new PaletteStruct {r= 255, g= 255, b= 102, a= 255},
            new PaletteStruct {r= 255, g= 255, b= 51, a= 255},
            new PaletteStruct {r= 255, g= 255, b= 0, a= 255},
            new PaletteStruct {r= 255, g= 204, b= 255, a= 255},
            new PaletteStruct {r= 255, g= 204, b= 204, a= 255},
            new PaletteStruct {r= 255, g= 204, b= 153, a= 255},
            new PaletteStruct {r= 255, g= 204, b= 102, a= 255},
            new PaletteStruct {r= 255, g= 204, b= 51, a= 255},
            new PaletteStruct {r= 255, g= 204, b= 0, a= 255},
            new PaletteStruct {r= 255, g= 153, b= 255, a= 255},
            new PaletteStruct {r= 255, g= 153, b= 204, a= 255},
            new PaletteStruct {r= 255, g= 153, b= 153, a= 255},
            new PaletteStruct {r= 255, g= 153, b= 102, a= 255},
            new PaletteStruct {r= 255, g= 153, b= 51, a= 255},
            new PaletteStruct {r= 255, g= 153, b= 0, a= 255},
            new PaletteStruct {r= 255, g= 102, b= 255, a= 255},
            new PaletteStruct {r= 255, g= 102, b= 204, a= 255},
            new PaletteStruct {r= 255, g= 102, b= 153, a= 255},
            new PaletteStruct {r= 255, g= 102, b= 102, a= 255},
            new PaletteStruct {r= 255, g= 102, b= 51, a= 255},
            new PaletteStruct {r= 255, g= 102, b= 0, a= 255},
            new PaletteStruct {r= 255, g= 51,  b= 255, a= 255},
            new PaletteStruct {r= 255, g= 51,  b= 204, a= 255},
            new PaletteStruct {r= 255, g= 51,  b= 153, a= 255},
            new PaletteStruct {r= 255, g= 51,  b= 102, a= 255},
            new PaletteStruct {r= 255, g= 51,  b= 51, a= 255},
            new PaletteStruct {r= 255, g= 51,  b= 0, a= 255},
            new PaletteStruct {r= 255, g= 0,   b= 255, a= 255},
            new PaletteStruct {r= 255, g= 0,   b= 204, a= 255},
            new PaletteStruct {r= 255, g= 0,   b= 153, a= 255},
            new PaletteStruct {r= 255, g= 0,   b= 102, a= 255},
            new PaletteStruct {r= 255, g= 0,   b= 51, a= 255},
            new PaletteStruct {r= 255, g= 0,   b= 0, a= 255},
            new PaletteStruct {r= 204, g= 255, b= 255, a= 255},
            new PaletteStruct {r= 204, g= 255, b= 204, a= 255},
            new PaletteStruct {r= 204, g= 255, b= 153, a= 255},
            new PaletteStruct {r= 204, g= 255, b= 102, a= 255},
            new PaletteStruct {r= 204, g= 255, b= 51, a= 255},
            new PaletteStruct {r= 204, g= 255, b= 0, a= 255},
            new PaletteStruct {r= 204, g= 204, b= 255, a= 255},
            new PaletteStruct {r= 204, g= 204, b= 204, a= 255},
            new PaletteStruct {r= 204, g= 204, b= 153, a= 255},
            new PaletteStruct {r= 204, g= 204, b= 102, a= 255},
            new PaletteStruct {r= 204, g= 204, b= 51, a= 255},
            new PaletteStruct {r= 204, g= 204, b= 0, a= 255},
            new PaletteStruct {r= 204, g= 153, b= 255, a= 255},
            new PaletteStruct {r= 204, g= 153, b= 204, a= 255},
            new PaletteStruct {r= 204, g= 153, b= 153, a= 255},
            new PaletteStruct {r= 204, g= 153, b= 102, a= 255},
            new PaletteStruct {r= 204, g= 153, b= 51, a= 255},
            new PaletteStruct {r= 204, g= 153, b= 0, a= 255},
            new PaletteStruct {r= 204, g= 102, b= 255, a= 255},
            new PaletteStruct {r= 204, g= 102, b= 204, a= 255},
            new PaletteStruct {r= 204, g= 102, b= 153, a= 255},
            new PaletteStruct {r= 204, g= 102, b= 102, a= 255},
            new PaletteStruct {r= 204, g= 102, b= 51, a= 255},
            new PaletteStruct {r= 204, g= 102, b= 0, a= 255},
            new PaletteStruct {r= 204, g= 51, b= 255, a= 255},
            new PaletteStruct {r= 204, g= 51, b= 204, a= 255},
            new PaletteStruct {r= 204, g= 51, b= 153, a= 255},
            new PaletteStruct {r= 204, g= 51, b= 102, a= 255},
            new PaletteStruct {r= 204, g= 51, b= 51, a= 255},
            new PaletteStruct {r= 204, g= 51, b= 0, a= 255},
            new PaletteStruct {r= 204, g= 0, b= 255, a= 255},
            new PaletteStruct {r= 204, g= 0, b= 204, a= 255},
            new PaletteStruct {r= 204, g= 0, b= 153, a= 255},
            new PaletteStruct {r= 204, g= 0, b= 102, a= 255},
            new PaletteStruct {r= 204, g= 0, b= 51, a= 255},
            new PaletteStruct {r= 204, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 153, g= 255, b= 255, a= 255},
            new PaletteStruct {r= 153, g= 255, b= 204, a= 255},
            new PaletteStruct {r= 153, g= 255, b= 153, a= 255},
            new PaletteStruct {r= 153, g= 255, b= 102, a= 255},
            new PaletteStruct {r= 153, g= 255, b= 51, a= 255},
            new PaletteStruct {r= 153, g= 255, b= 0, a= 255},
            new PaletteStruct {r= 153, g= 204, b= 255, a= 255},
            new PaletteStruct {r= 153, g= 204, b= 204, a= 255},
            new PaletteStruct {r= 153, g= 204, b= 153, a= 255},
            new PaletteStruct {r= 153, g= 204, b= 102, a= 255},
            new PaletteStruct {r= 153, g= 204, b= 51, a= 255},
            new PaletteStruct {r= 153, g= 204, b= 0, a= 255},
            new PaletteStruct {r= 153, g= 153, b= 255, a= 255},
            new PaletteStruct {r= 153, g= 153, b= 204, a= 255},
            new PaletteStruct {r= 153, g= 153, b= 153, a= 255},
            new PaletteStruct {r= 153, g= 153, b= 102, a= 255},
            new PaletteStruct {r= 153, g= 153, b= 51, a= 255},
            new PaletteStruct {r= 153, g= 153, b= 0, a= 255},
            new PaletteStruct {r= 153, g= 102, b= 255, a= 255},
            new PaletteStruct {r= 153, g= 102, b= 204, a= 255},
            new PaletteStruct {r= 153, g= 102, b= 153, a= 255},
            new PaletteStruct {r= 153, g= 102, b= 102, a= 255},
            new PaletteStruct {r= 153, g= 102, b= 51, a= 255},
            new PaletteStruct {r= 153, g= 102, b= 0, a= 255},
            new PaletteStruct {r= 153, g= 51, b= 255, a= 255},
            new PaletteStruct {r= 153, g= 51, b= 204, a= 255},
            new PaletteStruct {r= 153, g= 51, b= 153, a= 255},
            new PaletteStruct {r= 153, g= 51, b= 102, a= 255},
            new PaletteStruct {r= 153, g= 51, b= 51, a= 255},
            new PaletteStruct {r= 153, g= 51, b= 0, a= 255},
            new PaletteStruct {r= 153, g= 0, b= 255, a= 255},
            new PaletteStruct {r= 153, g= 0, b= 204, a= 255},
            new PaletteStruct {r= 153, g= 0, b= 153, a= 255},
            new PaletteStruct {r= 153, g= 0, b= 102, a= 255},
            new PaletteStruct {r= 153, g= 0, b= 51, a= 255},
            new PaletteStruct {r= 153, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 102, g= 255, b= 255, a= 255},
            new PaletteStruct {r= 102, g= 255, b= 204, a= 255},
            new PaletteStruct {r= 102, g= 255, b= 153, a= 255},
            new PaletteStruct {r= 102, g= 255, b= 102, a= 255},
            new PaletteStruct {r= 102, g= 255, b= 51, a= 255},
            new PaletteStruct {r= 102, g= 255, b= 0, a= 255},
            new PaletteStruct {r= 102, g= 204, b= 255, a= 255},
            new PaletteStruct {r= 102, g= 204, b= 204, a= 255},
            new PaletteStruct {r= 102, g= 204, b= 153, a= 255},
            new PaletteStruct {r= 102, g= 204, b= 102, a= 255},
            new PaletteStruct {r= 102, g= 204, b= 51, a= 255},
            new PaletteStruct {r= 102, g= 204, b= 0, a= 255},
            new PaletteStruct {r= 102, g= 153, b= 255, a= 255},
            new PaletteStruct {r= 102, g= 153, b= 204, a= 255},
            new PaletteStruct {r= 102, g= 153, b= 153, a= 255},
            new PaletteStruct {r= 102, g= 153, b= 102, a= 255},
            new PaletteStruct {r= 102, g= 153, b= 51, a= 255},
            new PaletteStruct {r= 102, g= 153, b= 0, a= 255},
            new PaletteStruct {r= 102, g= 102, b= 255, a= 255},
            new PaletteStruct {r= 102, g= 102, b= 204, a= 255},
            new PaletteStruct {r= 102, g= 102, b= 153, a= 255},
            new PaletteStruct {r= 102, g= 102, b= 102, a= 255},
            new PaletteStruct {r= 102, g= 102, b= 51, a= 255},
            new PaletteStruct {r= 102, g= 102, b= 0, a= 255},
            new PaletteStruct {r= 102, g= 51, b= 255, a= 255},
            new PaletteStruct {r= 102, g= 51, b= 204, a= 255},
            new PaletteStruct {r= 102, g= 51, b= 153, a= 255},
            new PaletteStruct {r= 102, g= 51, b= 102, a= 255},
            new PaletteStruct {r= 102, g= 51, b= 51, a= 255},
            new PaletteStruct {r= 102, g= 51, b= 0, a= 255},
            new PaletteStruct {r= 102, g= 0, b= 255, a= 255},
            new PaletteStruct {r= 102, g= 0, b= 204, a= 255},
            new PaletteStruct {r= 102, g= 0, b= 153, a= 255},
            new PaletteStruct {r= 102, g= 0, b= 102, a= 255},
            new PaletteStruct {r= 102, g= 0, b= 51, a= 255},
            new PaletteStruct {r= 102, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 51, g= 255, b= 255, a= 255},
            new PaletteStruct {r= 51, g= 255, b= 204, a= 255},
            new PaletteStruct {r= 51, g= 255, b= 153, a= 255},
            new PaletteStruct {r= 51, g= 255, b= 102, a= 255},
            new PaletteStruct {r= 51, g= 255, b= 51, a= 255},
            new PaletteStruct {r= 51, g= 255, b= 0, a= 255},
            new PaletteStruct {r= 51, g= 204, b= 255, a= 255},
            new PaletteStruct {r= 51, g= 204, b= 204, a= 255},
            new PaletteStruct {r= 51, g= 204, b= 153, a= 255},
            new PaletteStruct {r= 51, g= 204, b= 102, a= 255},
            new PaletteStruct {r= 51, g= 204, b= 51, a= 255},
            new PaletteStruct {r= 51, g= 204, b= 0, a= 255},
            new PaletteStruct {r= 51, g= 153, b= 255, a= 255},
            new PaletteStruct {r= 51, g= 153, b= 204, a= 255},
            new PaletteStruct {r= 51, g= 153, b= 153, a= 255},
            new PaletteStruct {r= 51, g= 153, b= 102, a= 255},
            new PaletteStruct {r= 51, g= 153, b= 51, a= 255},
            new PaletteStruct {r= 51, g= 153, b= 0, a= 255},
            new PaletteStruct {r= 51, g= 102, b= 255, a= 255},
            new PaletteStruct {r= 51, g= 102, b= 204, a= 255},
            new PaletteStruct {r= 51, g= 102, b= 153, a= 255},
            new PaletteStruct {r= 51, g= 102, b= 102, a= 255},
            new PaletteStruct {r= 51, g= 102, b= 51, a= 255},
            new PaletteStruct {r= 51, g= 102, b= 0, a= 255},
            new PaletteStruct {r= 51, g= 51, b= 255, a= 255},
            new PaletteStruct {r= 51, g= 51, b= 204, a= 255},
            new PaletteStruct {r= 51, g= 51, b= 153, a= 255},
            new PaletteStruct {r= 51, g= 51, b= 102, a= 255},
            new PaletteStruct {r= 51, g= 51, b= 51, a= 255},
            new PaletteStruct {r= 51, g= 51, b= 0, a= 255},
            new PaletteStruct {r= 51, g= 0, b= 255, a= 255},
            new PaletteStruct {r= 51, g= 0, b= 204, a= 255},
            new PaletteStruct {r= 51, g= 0, b= 153, a= 255},
            new PaletteStruct {r= 51, g= 0, b= 102, a= 255},
            new PaletteStruct {r= 51, g= 0, b= 51, a= 255},
            new PaletteStruct {r= 51, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 255, b= 255, a= 255},
            new PaletteStruct {r= 0, g= 255, b= 204, a= 255},
            new PaletteStruct {r= 0, g= 255, b= 153, a= 255},
            new PaletteStruct {r= 0, g= 255, b= 102, a= 255},
            new PaletteStruct {r= 0, g= 255, b= 51, a= 255},
            new PaletteStruct {r= 0, g= 255, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 204, b= 255, a= 255},
            new PaletteStruct {r= 0, g= 204, b= 204, a= 255},
            new PaletteStruct {r= 0, g= 204, b= 153, a= 255},
            new PaletteStruct {r= 0, g= 204, b= 102, a= 255},
            new PaletteStruct {r= 0, g= 204, b= 51, a= 255},
            new PaletteStruct {r= 0, g= 204, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 153, b= 255, a= 255},
            new PaletteStruct {r= 0, g= 153, b= 204, a= 255},
            new PaletteStruct {r= 0, g= 153, b= 153, a= 255},
            new PaletteStruct {r= 0, g= 153, b= 102, a= 255},
            new PaletteStruct {r= 0, g= 153, b= 51, a= 255},
            new PaletteStruct {r= 0, g= 153, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 102, b= 255, a= 255},
            new PaletteStruct {r= 0, g= 102, b= 204, a= 255},
            new PaletteStruct {r= 0, g= 102, b= 153, a= 255},
            new PaletteStruct {r= 0, g= 102, b= 102, a= 255},
            new PaletteStruct {r= 0, g= 102, b= 51, a= 255},
            new PaletteStruct {r= 0, g= 102, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 51, b= 255, a= 255},
            new PaletteStruct {r= 0, g= 51, b= 204, a= 255},
            new PaletteStruct {r= 0, g= 51, b= 153, a= 255},
            new PaletteStruct {r= 0, g= 51, b= 102, a= 255},
            new PaletteStruct {r= 0, g= 51, b= 51, a= 255},
            new PaletteStruct {r= 0, g= 51, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 255, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 204, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 153, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 102, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 51, a= 255},
            new PaletteStruct {r= 238, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 221, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 187, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 170, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 136, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 119, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 85, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 68, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 34, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 17, g= 0, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 238, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 221, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 187, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 170, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 136, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 119, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 85, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 68, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 34, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 17, b= 0, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 238, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 221, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 187, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 170, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 136, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 119, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 85, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 68, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 34, a= 255},
            new PaletteStruct {r= 0, g= 0, b= 17, a= 255},

            new PaletteStruct {r= 238, g= 238, b= 238, a= 255},

            new PaletteStruct {r= 221, g= 221, b= 221, a= 255},
            new PaletteStruct {r= 187, g= 187, b= 187, a= 255},
            new PaletteStruct {r= 170, g= 170, b= 170, a= 255},
            new PaletteStruct {r= 136, g= 136, b= 136, a= 255},
            new PaletteStruct {r= 119, g= 119, b= 119, a= 255},
            new PaletteStruct {r= 85, g= 85, b= 85, a= 255},
            new PaletteStruct {r= 68, g= 68, b= 68, a= 255},
            new PaletteStruct {r= 34, g= 34, b= 34, a= 255},
            new PaletteStruct {r= 17, g= 17, b= 17, a= 255},
            // {r=0,g=0,b=0,a=255},
        };
    }
}